<?php

namespace App\Service\OrderService\OrderRouteService;

use App\Service\Code\Normal;
use App\Service\OrderService\BaseSupplierRouterService;
use App\Model\PssModel\OrderSupplier;
use App\Model\PssModel\Business;
use App\Service\VendorClient\Shophltmsp\ShophltmspApiClient;
use Service\AppService\IdService;
use App\Service\TraitService\SupplierRouterOrderTrait;
use App\Model\CommodityModel\SupplierCommodity;

/***
 * 海旅会员购上游对接(一般贸易)
 * @author: hj
 */
class Shophltmsp extends BaseSupplierRouterService {

    use SupplierRouterOrderTrait;

    /**
     * 订单申报
     * @param array $param
     * @return array
     */
    public function orderPay($param = []) {
        $goodsList      = $param['orderGoods'];
        $supplierSkuIds = array_column($goodsList, 'supplier_skuid');
        $goodsIdArr     = SupplierCommodity::whereIn('ID', $supplierSkuIds)->pluck('SupplierCommodityKey', 'ID')->toArray();
        foreach ($param['orderGoods'] as &$item) {
            if (isset($goodsIdArr[$item['supplier_skuid']])) {
                $item['supplier_commodity_key'] = $goodsIdArr[$item['supplier_skuid']];
            }
            unset($item);
        }
        return $this->callSdkApi(ShophltmspApiClient::class, $param['apiBody'])->createOrder($param);
    }

    /**
     * 订单取消
     * @param array $param
     * @return array
     */
    public function orderCancel($param = []) {
        return Normal::returnCode(Normal::SUC, "处理完成");
    }

    /**
     * 订单售后
     * @param array $param
     * @return array
     */
    public function orderAfterSale($param = []) {
        return $this->callSdkApi(ShophltmspApiClient::class, $param['apiBody'])->{__FUNCTION__}($param);
    }

    /**
     * 获取物流信息
     * @param array $param
     */
    public function orderExpress($param = []) {
        return Normal::returnCode(Normal::SUC, "处理完成");
    }

    /**
     * 获取物流轨迹信息
     * @param array $param
     */
    public function queryExpressLogistics($param = []) {
        return $this->callSdkApi(ShophltmspApiClient::class, $param['apiBody'])->{__FUNCTION__}($param['queryBody']);
    }

    /**
     * 商品列表查询接口
     * @param array $param
     * @return array
     */
    public function queryGoodsList($param = []) {
        $res = $this->callSdkApi(ShophltmspApiClient::class, $param['apiBody'])->{__FUNCTION__}($param['queryBody']);
        if ($res['code'] != Normal::SUC) {
            return $res;
        }
        return Normal::returnCode(Normal::SUC, "获取成功", [
            'total'   => $res['data']['total'],
            'list'    => $res['data']['data'],
            'hasPage' => $res['data']['hasPage'] ?? 1
        ]);
    }

    /**
     * 商品详情查询接口
     * @param array $param
     * @return array
     */
    public function queryGoodsDetail($param = []) {
        return $this->callSdkApi(ShophltmspApiClient::class, $param['apiBody'])->{__FUNCTION__}($param['queryBody']);
    }

    /**
     * 订单详情查询接口
     * @param array $param
     * @return array
     */
    public function queryOrderDetail($param = []) {
        return $this->callSdkApi(ShophltmspApiClient::class, $param['apiBody'])->{__FUNCTION__}($param['queryBody']);
    }

}